import JsonSeriType from "../base/JsonSeriType"
import JsonSeriable from "../base/JsonSeriable"
import FIELD_VALUE_TYPE from "../constants/commonConstants"

export default class FieldGroup  extends JsonSeriable{
	#name;
	#code;
	#fieldList=[];
	constructor(name,code){
		super();
		this.#name = name;
		this.#code = code;
		super.seriType = new JsonSeriType(FIELD_VALUE_TYPE.base.FieldGroup.code,FIELD_VALUE_TYPE.base.FieldGroup.clsPage);
	}
	
	getFields(){
		return this.#fieldList;
	}
	getField(code){
		if(!code){
			return null;
		}
		for(let f of this.#fieldList){
			if(code == f.code){
				return f;
			}
		}
		return null;
	}
	getAllField(code){
		let ret = [];
		if(!code){
			return ret;
		}
		for(let f of this.#fieldList){
			if(code == f.code){
				ret.push(f);
			}
		}
		return ret;
	}
	addField(f){
		this.#fieldList.push(f);
	}
	removeField(f){
		if(!f){
			return;
		}
		for(let fix = 0; fix < this.#fieldList.length;fix++){
			if(f == this.#fieldList[fix]){
				this.#fieldList.splice(fix, 1);
				return ;
			}
		}
	}
	removeByCode(code,removeAll){
		if(!code){
			return;
		}
		
		for(let fix = 0; fix < this.#fieldList.length;fix++){
			if(code == this.#fieldList[fix].code){
				this.#fieldList.splice(fix, 1);
				if(!removeAll){
					return ;
				}
				fix--;
			}
		}
		
	}
	
	get name(){
		return this.#name;
	}
	set name(n){
		this.#name = n;
	}
	
	get code(){
		return this.#code;
	}
	set code(c){
		return this.#code = c;
	}
	
	get fieldList(){
		return this.#fieldList;
	}
	set fieldList(c){
		return this.#fieldList = c;
	}
	
	genSeriObj(){
		
		let ret={
			"name": this.#name,
			"code": this.#code,
			"fieldList":this.#fieldList
		};
		return ret;
	
	}
	
	seriObj(obj){
		this.#name =  obj.name;
		this.#code =  obj.code;
		this.#fieldList =  obj.fieldList;
	}
	
}